package hao.bk.parking;

import hao.bk.royalmap.GeneralAnimationUtils;
import hao.bk.royalmap.HeaderButton;
import hao.bk.royalmap.HeaderLayout;
import hao.bk.royalmap.R;
import hao.bk.royalmap.OnBackPressedInterface;
import hao.bk.royalmap.RoyalMapActivity;
import hao.bk.royalmap.SearchBarLayout;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;


/**
 * Hiện thị chức năng lưu vị trí đỗ xe
 * 
 * @author haoDV
 * 
 */
public class ParkingGeneralLayout extends RelativeLayout implements 
		OnClickListener,OnBackPressedInterface{

	private RoyalMapActivity main;
	public FrameLayout contentFrame;
	protected HeaderLayout contactHeader;
	protected SearchBarLayout searchBarLayout;
	/**Lưu lại vị trí đỗ xe */
	public HeaderButton tabSaveParking;

	/** Chức năng chụp ảnh lưu vị trí đỗ xe */
	private HeaderButton tabMapParking;

	/** trạng thái các tab  */
	public static final int TAB_SAVE_PARKING = 0;
	public static final int TAB_MAP_PARKING = 1;
	protected HeaderButton[] tabs = new HeaderButton[2];
	protected int activeTabs = TAB_SAVE_PARKING;
	public ParkingSaveLayout parkingSaveLayout ;
	public ParkingMapLayout parkingMapLayout;
	
	public ParkingGeneralLayout(Context context) {
		super(context);
		main = (RoyalMapActivity) context;
		inflate(main, R.layout.parking, this);
	}

	/**
	 * Khởi tạo các tham số cho Parking
	 */
	public void initParams() {
		contentFrame = (FrameLayout) findViewById(R.id.parking_content_frame);
		// khởi tạo 2 tab
		contactHeader = (HeaderLayout) findViewById(R.id.parking_tab);
		contactHeader.setHeaderTitle(main.getResources().getString(
				R.string.parking_header_back));
		LinearLayout view = new LinearLayout(main);
		view = (LinearLayout) inflate(main, R.layout.parking_tabs, view);
		contactHeader.addContentFrame(view);
		
		searchBarLayout = (SearchBarLayout)findViewById(R.id.searchmap_searchbar);

		tabSaveParking = (HeaderButton) findViewById(R.id.parking_tab_save_btn);
		tabSaveParking.setTextHeader(main.getResources().getString(
				R.string.parking_save_location));
		tabSaveParking.setOnClickListener(this);
		tabs[0] = tabSaveParking;
		
		tabMapParking = (HeaderButton) findViewById(R.id.parking_tab_camera_btn);
		tabMapParking.setTextHeader(main.getResources().getString(
				R.string.parking_camera_location));
		tabMapParking.setOnClickListener(this);
		tabs[1] = tabMapParking;
		updateActiveTab(TAB_SAVE_PARKING, true);
		activeTabs = TAB_SAVE_PARKING;
		
		updateParkingSave();
		main.setOnBackPressedInterface(this);		
	}
	
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.parking_tab_save_btn) {
			if (activeTabs == TAB_SAVE_PARKING) {
				return;
			}
			else if(activeTabs == TAB_MAP_PARKING){
				
			//hiển thị giao diện lưu vị trí đỗ xe
				updateParkingSave();
				GeneralAnimationUtils.startLeftToRight(parkingMapLayout, parkingSaveLayout);
				
				activeTabs = TAB_SAVE_PARKING;
			}
			updateActiveTab(TAB_SAVE_PARKING,true);
		} else if (id == R.id.parking_tab_camera_btn) {
			if (activeTabs == TAB_MAP_PARKING) {
				return;
			} else if(activeTabs == TAB_SAVE_PARKING){
	
				updateParkingMap();
				
				GeneralAnimationUtils.startRightToLeft(parkingSaveLayout, parkingMapLayout);
				activeTabs = TAB_MAP_PARKING;
				
			}
			updateActiveTab(TAB_MAP_PARKING, true);
		}
	}
	/**
	 * lưu vị trí đỗ xe
	 */
	public void updateParkingSave(){
		
		if(parkingSaveLayout == null) {
			
			parkingSaveLayout = new ParkingSaveLayout(main);
		}
		addContentFrame(parkingSaveLayout);
		//parkingSaveLayout.updateLocal();
	}

	/**
	 * Chức năng chụp ảnh
	 * @param view
	 */
	public void updateParkingMap(){
		if(parkingMapLayout == null){
			parkingMapLayout = new ParkingMapLayout(main);
			
		}
		addContentFrame(parkingMapLayout);
	}
			
	// Thêm nội dung vào khung nội dung
	public void addContentFrame(View view) {
		contentFrame.removeAllViews();
		RelativeLayout.LayoutParams param = new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		contentFrame.addView(view, param);
	}

	public void updateActiveTab(int tabIndex, boolean isActive) {
		activeTabs = tabIndex;

		if (tabIndex < 0 || tabIndex > 1) {
			return;
		}

		if (isActive) {
			for (int i = 0; i < tabs.length; i++) {
				if (tabIndex == i) {
					tabs[i].setHeaderFocus(true);
				} else {
					tabs[i].setHeaderFocus(false);
				}
			}
		} else {
			tabs[tabIndex].setHeaderFocus(false);
		}
	}

		@Override
	public void onBackPressed() {
		main.moveTabParkingToHome();
		main.setOnBackPressedInterface(null);
	}

}
